package com.ocp.socket.common.utils;

import com.alibaba.fastjson.JSON;
import com.example.app.base.ResultStatus;
import com.ocp.socket.busi.warnItem.bean.BaseWarnItem;
import com.ocp.socket.busi.warnMsg.bean.BaseWarnMsg;
import com.ocp.socket.busi.warnMsg.mapper.BaseWarnMsgMapper;
import com.ocp.socket.common.bean.DevInfo;
import com.ocp.socket.common.enums.CacheKeyEnum;
import com.ocp.socket.common.enums.MsgTypeEnum;
import com.ocp.socket.common.enums.ResultStatusEnum;
import com.ocp.socket.server.vo.MessageVo;
import org.apache.mina.core.session.IoSession;

import java.util.Map;
import java.util.UUID;

/**
 * @Auther: zhangzh
 * @Date: 2019/1/23 16:41
 * @Description:
 * @Version: 1.0.0
 */
public class CommonUtils {
    //高位在前，低位在后
    public static byte[] int2bytes(int num){
        byte[] result = new byte[4];
        result[0] = (byte)((num >>> 24) & 0xff);//说明一
        result[1] = (byte)((num >>> 16)& 0xff );
        result[2] = (byte)((num >>> 8) & 0xff );
        result[3] = (byte)((num >>> 0) & 0xff );
        return result;
    }

    //高位在前，低位在后
    public static int bytes2int(byte[] bytes){
        int result = 0;
        if(bytes.length == 4){
            int a = (bytes[0] & 0xff) << 24;//说明二
            int b = (bytes[1] & 0xff) << 16;
            int c = (bytes[2] & 0xff) << 8;
            int d = (bytes[3] & 0xff);
            result = a | b | c | d;
        }
        return result;
    }

    public static void main(String[] args){
        int a = -64;
        System.out.println("-64="+Integer.toBinaryString(-64));
        byte[] bytes = CommonUtils.int2bytes(a);
        for(int i = 0 ; i<4 ; i++){
            System.out.println(bytes[i]);
        }
        a = CommonUtils.bytes2int(bytes);
        System.out.println(a);

    }

    /********
     * 插入报警信息
     * @param baseWarnMsgMapper
     * @param warnCode
     * @param baseWarnItemMap
     * @param messageVo
     * @param devInfo
     * @param snNo
     * @param lineCode
     * @param typeCode
     */
    public static void insertBaseWarnMsg(IoSession session,BaseWarnMsgMapper baseWarnMsgMapper, String warnCode,
                                         Map<String,BaseWarnItem> baseWarnItemMap, MessageVo messageVo,
                                         DevInfo devInfo, String snNo, String lineCode, RedisUtil redisUtil,String typeCode){
        BaseWarnMsg baseWarnMsg = new BaseWarnMsg();
        baseWarnMsg.setId(CommonUtils.getUUID());
        //baseWarnMsg.setId("aa");
        baseWarnMsg.setWarnCode(warnCode);
        if(snNo !=null && !"".equals(snNo)){
            baseWarnMsg.setSnNo(snNo);
        }
        if(lineCode !=null && !"".equals(lineCode)){
            baseWarnMsg.setLineCode(lineCode);
        }
        baseWarnMsg.setWarnName(baseWarnItemMap.get(warnCode).getWarnName());
        baseWarnMsg.setWarnNameEn(baseWarnItemMap.get(warnCode).getWarnNameEn());
        baseWarnMsg.setDataTime(messageVo.getDataTime());
        baseWarnMsg.setStartTime(messageVo.getDataTime());
        if(messageVo.isEndTime()){
            baseWarnMsg.setEndTime(messageVo.getDataTime());
        }
        baseWarnMsg.setMilkHallId(devInfo.getHallId());
        baseWarnMsg.setOrgId(devInfo.getOrgId());
        baseWarnMsg.setWarnContent(baseWarnItemMap.get(warnCode).getWarnContent());
        baseWarnMsg.setWarnContentEn(baseWarnItemMap.get(warnCode).getWarnContentEn());
        baseWarnMsg.setWarnReason(baseWarnItemMap.get(warnCode).getWarnReason());
        baseWarnMsg.setWarnReasonEn(baseWarnItemMap.get(warnCode).getWarnReasonEn());
        baseWarnMsg.setTypeCode(typeCode);
        baseWarnMsgMapper.insertBaseWarnMsg(baseWarnMsg);
        SessionUtil.sendAppUserMsgByOrgId(session,devInfo.getOrgId(),CommonUtils.toMsg(MsgTypeEnum.G.getTypeCode(),messageVo.getSn(),messageVo.getDataTime(),JSON.toJSON(baseWarnMsg).toString()));
        //放入消息队列
        redisUtil.rPush(CacheKeyEnum.WARN_MSG_PUSH_LIST.getCacheKey(), JSON.toJSONString(baseWarnMsg));
    }

    public static void updateBaseWarnMsgEndTimeById(IoSession session,BaseWarnMsgMapper baseWarnMsgMapper,String id,String dataTime,String snNo,Integer orgId){
        int cnt = baseWarnMsgMapper.updateBaseWarnMsgEndTimeById(id,dataTime);
        if(cnt>0){
            ResultStatus resultStatus = new ResultStatus(ResultStatusEnum.COMMON_SUCCESS,id);
            SessionUtil.sendAppUserMsgByOrgId(session,orgId,CommonUtils.toMsg(MsgTypeEnum.K.getTypeCode(),snNo,dataTime,JSON.toJSON(resultStatus).toString()));
        }

    }

    /********
     *
     * @param modelType
     * @param snNo
     * @param dataTime
     * @param msg
     * @return
     */
    public static String toMsg(String modelType,String snNo,String dataTime,String msg){
        //返回信息拼接
        StringBuffer returnVal = new StringBuffer();
        returnVal.append(modelType);
        returnVal.append(snNo);
        returnVal.append("DATA");
        returnVal.append(dataTime);
        returnVal.append(msg);
        return returnVal.toString();
    }

    /**
     * 获得一个UUID
     * @return String UUID
     */
    public static String getUUID(){
        String uuid = UUID.randomUUID().toString();
        //去掉“-”符号
        return uuid.replaceAll("-", "");
    }
}
